package pkg.andru.util;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.util.Log;

/**
 * dialog utils
 * <p>
 * <b>if any int agrument, you dont supply, just use -1 as default</b>
 * 
 * @author idiottiger
 * 
 */
public class DialogUtils {

    static final String TAG = "DialogUtils";

    static Dialog mDialog;

    /**
     * 
     * @param context
     * @param dialogId
     *            the dialog id, suggestion supply this for indentity the
     *            different dialog in {@link OnDialogEventListener}
     * @param titleId
     *            the tilte string id
     * @param messageId
     *            the dialog message string id
     * @param icon
     *            the icon resource id
     * @param positiveBtnId
     *            the positive button id, if no supply, will use
     *            {@link DialogInterface#BUTTON_POSITIVE}
     * @param positiveBtnStrId
     *            the positive button string id
     * @param middleBtnId
     *            the middle button id, if no supply, will use
     *            {@link DialogInterface#BUTTON_NEUTRAL}
     * @param middleBtnStrid
     *            the middle button string id
     * @param negativeBtnId
     *            the negative button id, if no supply, will use
     *            {@link DialogInterface#BUTTON_NEGATIVE}
     * @param negativeBtnStrId
     *            the negative button string id
     * @param cancelable
     *            check the cancelable
     * @param listener
     *            the dialog onclick listener
     * @param btnCloseDialog
     *            check the press any button will auto close the dialog or not
     */
    public static void showDialog(Context context, final int dialogId, int titleId, int messageId, int icon, int positiveBtnId, int positiveBtnStrId, int middleBtnId, int middleBtnStrid,
            int negativeBtnId, int negativeBtnStrId, boolean cancelable, OnDialogEventListener listener, boolean btnCloseDialog) {
        String title = null, message = null, posBtnStr = null, middBtnStr = null, negBtnStr = null;
        if (titleId != -1) {
            title = context.getString(titleId);
        }
        if (messageId != -1) {
            message = context.getString(messageId);
        }
        if (positiveBtnStrId != -1) {
            posBtnStr = context.getString(positiveBtnStrId);
        }
        if (middleBtnStrid != -1) {
            middBtnStr = context.getString(middleBtnStrid);
        }
        if (negativeBtnStrId != -1) {
            negBtnStr = context.getString(negativeBtnStrId);
        }

        showDialog(context, dialogId, title, message, icon, positiveBtnId, posBtnStr, middleBtnId, middBtnStr, negativeBtnId, negBtnStr, cancelable, listener, btnCloseDialog);
    }

    /**
     * 
     * @param context
     * @param dialogId
     * @param title
     * @param message
     * @param icon
     * @param positiveBtnId
     * @param positiveBtnStr
     * @param middleBtnId
     * @param middleBtnStr
     * @param negativeBtnId
     * @param negativeBtnStr
     * @param cancelable
     * @param listener
     * @param btnCloseDialog
     * 
     * @see #showDialog(Context, int, int, int, int, int, int, int, int, int,
     *      int, boolean, OnDialogEventListener, boolean)
     */
    public static void showDialog(Context context, final int dialogId, String title, String message, int icon, int positiveBtnId, String positiveBtnStr, int middleBtnId, String middleBtnStr,
            int negativeBtnId, String negativeBtnStr, boolean cancelable, OnDialogEventListener listener, final boolean btnCloseDialog) {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }

        final OnDialogEventListener finalListener = listener;
        final int pBtnid = positiveBtnId == -1 ? DialogInterface.BUTTON_POSITIVE : positiveBtnId;
        final int mBtnid = middleBtnId == -1 ? DialogInterface.BUTTON_NEUTRAL : middleBtnId;
        final int nBtnid = negativeBtnId == -1 ? DialogInterface.BUTTON_NEGATIVE : negativeBtnId;

        OnClickListener mlistener = new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // if click any button will close the dialog
                if (btnCloseDialog) {
                    dialog.dismiss();
                    if (mDialog != null) {
                        mDialog = null;
                    }
                }
                int resultWhich;
                if (which == DialogInterface.BUTTON_POSITIVE) {
                    resultWhich = pBtnid;
                } else if (which == DialogInterface.BUTTON_NEUTRAL) {
                    resultWhich = mBtnid;
                } else {
                    resultWhich = nBtnid;
                }

                if (finalListener != null) {
                    finalListener.onClick(dialog, dialogId, resultWhich);
                }
            }
        };

        OnCancelListener mCacnelListener = new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if (finalListener != null) {
                    finalListener.onCancel(dialog, dialogId);
                }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        if (title != null) {
            builder.setTitle(title);
        }
        if (message != null) {
            builder.setMessage(message);
        }
        if (positiveBtnStr != null) {
            builder.setPositiveButton(positiveBtnStr, mlistener);
        }
        if (middleBtnStr != null) {
            builder.setNeutralButton(middleBtnStr, mlistener);
        }
        if (negativeBtnStr != null) {
            builder.setNegativeButton(negativeBtnStr, mlistener);
        }
        if (icon != -1) {
            builder.setIcon(icon);
        }

        Dialog dialog = builder.create();
        dialog.setCancelable(cancelable);
        dialog.setOnCancelListener(mCacnelListener);

        mDialog = dialog;
        try {
            dialog.show();
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            dialog.cancel();
        }
    }

    /**
     * 
     * @param context
     * @param dialogId
     * @param titleId
     * @param messageId
     * @param icon
     * @param positiveBtnId
     * @param positiveBtnStrId
     * @param cancelable
     * @param listener
     * @param btnCloseDialog
     */
    public static void show1BtnDialog(Context context, final int dialogId, int titleId, int messageId, int icon, int positiveBtnId, int positiveBtnStrId, boolean cancelable,
            OnDialogEventListener listener, boolean btnCloseDialog) {
        showDialog(context, dialogId, titleId, messageId, icon, positiveBtnId, positiveBtnStrId, -1, -1, -1, -1, cancelable, listener, btnCloseDialog);
    }

    /**
     * dialog event listener, contain onclick and oncancel
     * 
     * @author idiottiger
     * 
     */
    public static interface OnDialogEventListener {

        /**
         * 
         * @param dialog
         * @param dialogId
         * @param buttonId
         *            if no supply will be
         *            {@link DialogInterface#BUTTON_POSITIVE} or
         *            {@link DialogInterface#BUTTON_NEUTRAL} or
         *            {@link DialogInterface#BUTTON_NEGATIVE}
         */
        public void onClick(DialogInterface dialog, int dialogId, int buttonId);

        /**
         * @see {@link DialogInterface.OnCancelListener#onCancel(DialogInterface)}
         * @param dialog
         */
        public void onCancel(DialogInterface dialog, int dialogId);
    }

}
